home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / daemons / init / sysvinit.000 / sysvinit / sysvinit-2.64 / dowall.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-02  |  2.9 KB  |  136 lines

  1. /*
  2.  * dowall.c    - Write to all users on the system.
  3.  *
  4.  * Author:      Miquel van Smoorenburg, miquels@drinkel.cistron.nl
  5.  * 
  6.  * Version:      1.01  18-11-1992
  7.  *          - initial version.
  8.  *
  9.  *          1.1   31-01-1993
  10.  *          - Made the open() non blocking, so that false utmp
  11.  *            entries will not block wall.
  12.  *
  13.  *          1.2   13-05-1993
  14.  *          - Added some more code to prevent 'hanging' on
  15.  *            open()'s or write()'s.
  16.  *
  17.  *        This file is part of the sysvinit suite,
  18.  *        Copyright 1991-1995 Miquel van Smoorenburg.
  19.  *
  20.  *        This program is free software; you can redistribute it and/or
  21.  *        modify it under the terms of the GNU General Public License
  22.  *        as published by the Free Software Foundation; either version
  23.  *        2 of the License, or (at your option) any later version.
  24.  */
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. #include <utmp.h>
  33. #include <pwd.h>
  34. #include <fcntl.h>
  35. #include <signal.h>
  36. #include <setjmp.h>
  37.  
  38. static jmp_buf jbuf;
  39.  
  40. #define AEROSMITH
  41.  
  42. /* Alarm handler */
  43. /*ARGSUSED*/
  44. static void handler(arg)
  45. int arg;
  46. {
  47.   signal(SIGALRM, handler);
  48.   longjmp(jbuf, 1);
  49. }
  50.  
  51. /*
  52.  * Wall function.
  53.  */
  54. void wall(text, fromshutdown)
  55. char *text;
  56. int fromshutdown;
  57. {
  58.   FILE *fp, *tp;
  59.   char line[81];
  60.   char term[32];
  61.   static char *user, ttynm[16], *date;
  62.   static int fd, init = 0;
  63.   struct passwd *pwd;
  64.   char *tty, *p;
  65.   time_t t;
  66.   struct utmp utmp;
  67.   
  68.   if ((fp = fopen(UTMP_FILE, "r")) == (FILE *)0) return;
  69.   
  70.   if (init == 0) {
  71.       if ((pwd = getpwuid(getuid())) == (struct passwd *)0) {
  72.           fprintf(stderr, "You don't exist. Go away.\n");
  73.           exit(1);
  74.       }
  75.       user = pwd->pw_name;
  76.       if ((p = ttyname(0)) != (char *)0) {
  77.           if ((tty = strrchr(p, '/')) != NULL)
  78.               tty++;
  79.           else
  80.               tty = p;
  81.           sprintf(ttynm, "(%s) ", tty);    
  82.       } else
  83.           ttynm[0] = 0;
  84.       init++;
  85.     signal(SIGALRM, handler);
  86.   }
  87.   
  88.   /* Get the time */
  89.   time(&t);
  90.   date = ctime(&t);
  91.   for(p = date; *p && *p != '\n'; p++)
  92.       ;
  93.   *p = 0;
  94.  
  95.   sprintf(line, "\007\r\nBroadcast message from %s %s%s...\r\n\r\n", user,
  96.       ttynm, date);
  97.  
  98.   while(fread(&utmp, sizeof(struct utmp), 1, fp) == 1) {
  99.       if(utmp.ut_type != USER_PROCESS ||
  100.        utmp.ut_user[0] == 0) continue;
  101.       sprintf(term, "/dev/%s", utmp.ut_line);
  102.  
  103.     alarm(2); /* Sometimes the open/write hangs in spite of the O_NDELAY */
  104. #ifdef O_NDELAY
  105.     /* Open it non-delay */
  106.     if (setjmp(jbuf) == 0 && (fd = open(term, O_WRONLY | O_NDELAY )) > 0) {
  107.           if ((tp = fdopen(fd, "w")) != NULL) {
  108.               fputs(line, tp);
  109.               fputs(text, tp);
  110. #ifdef AEROSMITH
  111.             if (fromshutdown && !strcmp(utmp.ut_user, "tyler"))
  112.              fputs("(Well hello Mr. Tyler - going DOWN?)\r\n", tp);
  113. #endif
  114.               fclose(tp);
  115.           } else
  116.             close(fd);
  117.         fd = -1;
  118.         alarm(0);
  119.     }
  120.     if (fd >= 0) close(fd);
  121. #else
  122.       if (setjmp(jbuf) == 0 && (tp = fopen(term, "w")) != NULL) {
  123.           fputs(line, tp);
  124.           fputs(text, tp);
  125.         alarm(0);
  126.           fclose(tp);
  127.         tp = NULL;
  128.       }
  129.     if (tp != NULL) fclose(tp);
  130. #endif
  131.     alarm(0);
  132.   }
  133.   fclose(fp);
  134. }
  135.  
  136.